package birthday.syj.com.manage.common.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;


import org.litepal.crud.DataSupport;



import java.util.List;

import com.joanzapata.android.BaseAdapterHelper;
import com.joanzapata.android.QuickAdapter;

import birthday.syj.com.manage.DB.GroupBean;
import birthday.syj.com.manage.R;




public class GroupListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{

    Toolbar toolbar;

    ListView mListView;

    private QuickAdapter<GroupBean> quickAdapter;

    private String tpye;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group);
        mListView= (ListView) findViewById(R.id.listview);
        tpye=getIntent().getStringExtra("tpye");
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        //为toolbar设置支持
        setSupportActionBar(toolbar);
        toolbar.setTitle("分组");
        mListView.setOnItemClickListener(this);
        initAdapter();
        initData();
    }

    private void initData() {
        List<GroupBean> list = DataSupport.findAll(GroupBean.class);
        quickAdapter.clear();
        quickAdapter.addAll(list);
        quickAdapter.notifyDataSetChanged();
    }

    private void initAdapter() {

        quickAdapter = new QuickAdapter<GroupBean>(this, R.layout.activity_list_item) {

            @Override
            protected void convert(BaseAdapterHelper baseAdapterHelper, final GroupBean bean) {

                baseAdapterHelper.setText(R.id.name, bean.getGroupname());

                TextView update=baseAdapterHelper.getView(R.id.update);

                TextView delete=baseAdapterHelper.getView(R.id.delete);

                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        DataSupport.deleteAll(GroupBean.class,"id=?",bean.getId()+"");//会把符合条件的数据删除掉
                        initData();
                    }
                });

                update.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showBottomSheetDialog(bean);
                    }
                });

            }
        };

        quickAdapter.clear();
        mListView.setAdapter(quickAdapter);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        GroupBean groupBean = quickAdapter.getItem(position);

        if ("sel".equals(tpye)){
            Intent intent=new Intent();
            intent.putExtra("bean",groupBean);
            setResult(1002,intent);
            finish();
        }else{
            Intent intent=new Intent(this,PeopleListActivity.class);
            intent.putExtra("bean",groupBean);
            startActivity(intent);
        }

    }

    //toolbar的选择菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar_add, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add:
                showBottomSheetDialog();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 底部弹框选择提醒时间
     */
    private void showBottomSheetDialog() {

        final BottomSheetDialog sheetDialog = new BottomSheetDialog(this);

        View dialogView = LayoutInflater.from(this).inflate(R.layout.group_add, null);

        Button buttonCancel = (Button) dialogView.findViewById(R.id.btnCancel);

        final EditText group_name = (EditText) dialogView.findViewById(R.id.group_name);

        Button buttonSure = (Button) dialogView.findViewById(R.id.btnSure);

        buttonSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if("".equals(group_name.getText().toString())){
                    return;
                }

                GroupBean groupBean=new GroupBean();
                groupBean.setGroupname(group_name.getText().toString());
                groupBean.save();
                initData();
                sheetDialog.dismiss();
            }
        });

        buttonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sheetDialog.dismiss();
            }
        });
        sheetDialog.setContentView(dialogView);
        sheetDialog.show();
    }

    /**
     * 底部弹框选择提醒时间
     */
    private void showBottomSheetDialog(final GroupBean groupBean) {

        final BottomSheetDialog sheetDialog = new BottomSheetDialog(this);

        View dialogView = LayoutInflater.from(this).inflate(R.layout.group_add, null);

        Button buttonCancel = (Button) dialogView.findViewById(R.id.btnCancel);

        final EditText group_name = (EditText) dialogView.findViewById(R.id.group_name);
        group_name.setText(groupBean.getGroupname());
        Button buttonSure = (Button) dialogView.findViewById(R.id.btnSure);

        buttonSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if("".equals(group_name.getText().toString())){
                    return;
                }


                groupBean.setGroupname(group_name.getText().toString());
                groupBean.update(groupBean.getId());
                initData();
                sheetDialog.dismiss();
            }
        });

        buttonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sheetDialog.dismiss();
            }
        });
        sheetDialog.setContentView(dialogView);
        sheetDialog.show();
    }

}
